package cn.schoolwow.util.module.query.jsonarray.flow.get;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonobject.QueryJSONObject;
import cn.schoolwow.util.module.query.jsonarray.domain.JSONArrayQueryOption;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GetJSONArrayMultipleFieldValueListBusiness implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONArrayQueryOption jsonQueryOption = flowContext.checkInstanceData(JSONArrayQueryOption.class);
        JSONArray filterArray = (JSONArray) flowContext.checkData("filterArray");
        Collection<String> fieldNames = flowContext.checkData("fieldNames", Collection.class);

        if(null==filterArray||filterArray.isEmpty()){
            return;
        }
        List fieldValueList = new ArrayList();
        for(int i=0;i<filterArray.size();i++){
            JSONObject object = filterArray.getJSONObject(i);

            Object fieldValue = QueryJSONObject.newQuery(object)
                    .execute()
                    .getMultipleFieldValue(fieldNames);
            if(null==fieldValue){
                continue;
            }
            if(jsonQueryOption.distinct&&fieldValueList.contains(fieldValue)){
                continue;
            }
            fieldValueList.add(fieldValue);
        }
        flowContext.putReturnData("fieldValueList", fieldValueList);
    }

    @Override
    public String name() {
        return "获取JSON数组多个字段值列表";
    }
}